<template>
  <div>
    <div class="login">
      <div class="login-head">
        <span>登录</span>
      </div>
      <div class="login-content">
        <div class="usercount">
          <input
            type="text"
            v-model="loginuser.id"
            placeholder="请输入您的账号"
          />
        </div>
        <div class="password">
          <input
            type="password"
            v-model="loginuser.password"
            placeholder="请输入您的密码"
          />
        </div>
      </div>
      <div class="login-bottom">
        <div class="login-button">
          <span @click="login">登录</span>
        </div>
        <div class="register-button">
          <router-link to="/register"><span>注册</span></router-link>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import { Dialog } from "vant";
import { mapState } from "vuex";
import "../http/request";
import Vue from "vue";
import { loginFunc } from "../../../../vuex day3/vote_web/src/http/request";
// 全局注册
Vue.use(Dialog);
export default {
  components: {
    [Dialog.Component.name]: Dialog.Component,
  },
  name: "login",
  data() {
    return {
      loginuser: {
        id: "",
        password: "",
      },
    };
  },
  computed: {
    ...mapState(["user"]),
  },
  methods: {
    login() {
      axios
        .post("http://192.168.35.11:3005/user", this.loginuser, {
          headers: "application/x-www-form-urlencoded",
        })
        .then((response) => {
          console.log(this.user);
          console.log(response.data.result);
          let result = response.data.result;
          let { userid, username, email, usercover } = result[0];
          localStorage.setItem("userid", userid);
          localStorage.setItem("username", username);
          localStorage.setItem("email", email);
          localStorage.setItem("usercover", usercover);

          console.log(this.$store);
          this.$store.dispatch("user/setEmailAsync", email);
          this.$store.dispatch("user/setuserIdAsync", userid);
          this.$store.dispatch("user/setUserNameAsync", username);
          if (result.length > 0) {
            Dialog.alert({
              title: "是否跳转个人中心页面?",
              //   message: 'data.msg',
            }).then(() => {
              // 页面跳转(传递参数)
              this.$router.push({
                name: "usercenter",
                params: { userid, username, email },
              });
            });
          } else {
            Dialog.alert({
              title: "是否跳转个人中心页面?",
              //   message: 'data.msg',
            }).then(() => {
              // 页面跳转(传递参数)
              this.$router.push({
                name: "login",
                params: { userid, username, email },
              });
            });
          }
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.login {
  box-sizing: border-box;
  width: 355px;
  height: 550px;
  margin: 50px auto;
  border: 2px solid #fafafa;
  // background: url(../assets/images/product/product3.jpg);
  .login-head {
    box-sizing: border-box;
    color: #777;
    width: 355px;
    height: 40px;
    margin: 50px auto;
  }
  .password {
    margin: 10px 0px;
    input {
      width: 200px;
      text-align: center;
      font-size: 14px;
      border: none;
      border-bottom: 1px solid #ccc;
      border-radius: 3px;
      padding: 10px;
    }
  }

  .usercount {
    border: none;
    margin-bottom: 20px;
    box-sizing: border-box;
    input {
      width: 200px;
      text-align: center;
      font-size: 14px;
      border: none;
      border-bottom: 1px solid #ccc;
      border-radius: 3px;
      padding: 10px;
      color: #777;
    }
  }
  .login-button {
    width: 250px;
    border-radius: 30px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    // margin-top:10px;
    margin: 30px auto;
    color: #fff;
    font-weight: 808;
    background-color: orange;
  }
  .register-button {
    width: 250px;
    border-radius: 50px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    margin: 0 auto;
    background-color: #777;
    color: #ccc;
    margin-bottom: 10px;
  }
}
</style>
